<template>
	<div>
		<!-- 面包屑导航 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>管理员管理</el-breadcrumb-item>
			<el-breadcrumb-item>管理员列表</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 卡片区 -->
		<el-card>
			<!-- 表格上部搜索，添加用户区 -->
			<el-row :gutter="20">
				<el-col :span="6">
					<el-input placeholder="请输入内容" v-model="searchStr" >
						<el-button slot="append" icon="el-icon-search" v-on:click="search(searchStr)"></el-button>
					</el-input>
				</el-col>
				<el-col :span="6">
					<el-button type="primary" v-on:click="addDialogVisible=true">添加管理员</el-button>
				</el-col>
			</el-row>
			<!-- 表格 -->
			<el-table :data="managerList" style="width: 100%" border stripe>
				<el-table-column type="index" label="#"></el-table-column>
				<el-table-column prop="manager_id" label="管理员ID"></el-table-column>
				<el-table-column prop="manager_email" label="管理员邮箱"></el-table-column>
				<el-table-column prop="manager_name" label="管理员名"></el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button type="primary" icon="el-icon-edit" size="mini" @click="openEditDialog(scope.row.manager_id)"></el-button>
						<el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteById(scope.row.manager_id)"></el-button>
						<el-tooltip class="item" effect="dark" content="分配权限" placement="top">
							<el-button type="warning" icon="el-icon-setting" size="mini"></el-button>
						</el-tooltip>

					</template>
				</el-table-column>
			</el-table>
			<!-- 分页区 -->
			<div class="block">
				<el-pagination @current-change="handleCurrentChange" :current-page.sync="pagenum" :page-size="5" layout="prev, pager, next, jumper, total"
				 :total="managernum">
				</el-pagination>
			</div>
		</el-card>
		<!-- 添加用户的弹出对话框 -->
		<el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addFormClose()">
			<!-- 主体区 -->
			<el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="70px" >
			  <el-form-item  label="用户名" prop="manager_name">
			    <el-input v-model="addForm.manager_name"></el-input>
			  </el-form-item>
			  <el-form-item  label="邮箱" prop="manager_email">
			    <el-input v-model="addForm.manager_email" ></el-input>
			  </el-form-item>
			  <el-form-item  label="密码" prop="manager_password">
			     <el-input  v-model="addForm.manager_password" show-password></el-input>
			  </el-form-item>
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取消</el-button>
				<el-button type="primary"  @click="addmanager()">确定</el-button>
			</span>
		</el-dialog>
		<!-- 编辑用户的弹出对话框 -->
		<el-dialog title="编辑用户" :visible.sync="editDialogVisible" width="50%" >
			<!-- 主体区 -->
			<el-form :model="editForm" :rules="editFormRules" ref="editFormRef" label-width="70px" >
			  <el-form-item  label="用户名" prop="manager_name">
			    <el-input v-model="editForm.manager_name" disable></el-input>
			  </el-form-item>
			  <el-form-item  label="邮箱" prop="manager_email">
			    <el-input v-model="editForm.manager_email" ></el-input>
			  </el-form-item>
			  <el-form-item  label="密码" prop="manager_password">
			     <el-input  v-model="editForm.manager_password" show-password></el-input>
			  </el-form-item>
			</el-form>
			<!-- 底部区域 -->
			<span slot="footer" class="dialog-footer">
				<el-button @click="addDialogVisible = false">取消</el-button>
				<el-button type="primary"  @click="editmanager()">确定</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	export default {
		created() {
			this.getmanagerList(1, '')

		},
		data() {
			return {
				searchStr: '',
				managerList: [],
				//当前所在页码
				pagenum: 0,
				//数据共多少页
				totalpage: 0,
				//数据条数
				managernum: 0,
				//控制用户添加对话框的可见与否
				addDialogVisible: false,
				//添加新用户表单
				addForm:{
					manager_name:'',
					manager_email:'',
					manager_password:''
				},
				//添加新用户的表单规则
				addFormRules:{
					manager_name:[
						{required:true,message:"请输入用户名",trigger:"blur"},
						{min:3,max:10,message:"用户名长度在3-10之间",trigger:"blur"}
					],
					manager_email:[
						{required:true,message:"请输入邮箱",trigger:"blur"},
						{ type:"email",required:true,message:"邮箱格式错误",trigger:"blur"},
					],
					manager_password:[
						{required:true,message:"请输入密码",trigger:"blur"}						
					]
				},
				
				//编辑用户弹出框的可见与否
				editDialogVisible:false,
				//编辑表单
				editForm:{
					manager_name:'',
					manager_email:'',
					manager_password:''
				},
				//编辑用户的表单规则和新增的表单规则相同
				editFormRules:{
					manager_name:[
						{required:true,message:"请输入用户名",trigger:"blur"},
						{min:3,max:10,message:"用户名长度在3-10之间",trigger:"blur"}
					],
					manager_email:[
						{required:true,message:"请输入邮箱",trigger:"blur"},
						{ type:"email",required:true,message:"邮箱格式错误",trigger:"blur"},
					],
					manager_password:[
						{required:true,message:"请输入密码",trigger:"blur"}						
					]
				},
				//编辑的用户的id
				editmanagerId:0
					
			}
		},
		methods: {
			//拿到表格展示数据
			getmanagerList(pagenum, searchStr) {
				this.$axios
					.get("/manager?pagenum=" + pagenum + "&searchStr=" + searchStr)
					.then(backinfo => {
						//要用的数据在backinfo.data中,把这个值给res。该写法是ES6解构对象。
						const {data: res} = backinfo
						if (res.err_code == 0) {
							this.managerList = res.data.managers
							this.pagenum = res.data.pagenum
							this.totalpage = res.data.totalpage
							this.managernum = res.data.managernum
							//return this.$message.success(res.message)
						}else{
							this.managerList = []
							this.pagenum = 1
							this.totalpage = 1
							this.managernum = 0
						}
					})
			},
			//页面改变调用,自带新页的页码做参数
			handleCurrentChange(newpagenum) {
				this.getmanagerList(newpagenum, this.searchStr)
			},
			//搜索
			search(searchStr) {
				this.getmanagerList(1, searchStr)
			},
			//添加用户
			addmanager(){
				this.$refs.addFormRef.validate(valid=>{
					if(!valid){//格式验证不对，res为flase
						return
					}
					console.log("用户添加表单验证成功")
					var res= this.$axios
										.post('/manager',this.addForm)		
										.then((backinfo)=>{									
										//	console.log(backinfo)	
											const {data:res}=backinfo
											if(res.err_code==1){
												//添加用户失败
												this.$message.error(res.message)
											}else{
												//添加用户成功
												this.$message.success(res.message)
												this.addDialogVisible = false 
												this.getmanagerList(1,'')
										    }
										})					
				})
				 
			},
			//关闭对话框（即可见性为false）触发，清空填入数据
			addFormClose(){
				this.$refs.addFormRef.resetFields()
			},
			//删除数据
			deleteById(manager_id){
				this.$axios
					.delete("/manager?manager_id="+manager_id)
					.then(backinfo => {
						//要用的数据在backinfo.data中,把这个值给res。该写法是ES6解构对象。
						const {data: res} = backinfo
						if (res.err_code == 0) {
							this.$message.success(res.message)
							return this.getmanagerList(1,'')
						}else{
							return this.$message.error(res.message)
							
							
						}
					})
				
			},
			//打开编辑数据对话框,把要编辑用户的数据带进去
			openEditDialog(manager_id){
				//console.log(manager_id)
				this.editmanagerId=manager_id
				this.editDialogVisible=true
				this.$axios
							.get("manager/"+manager_id)
							.then(backinfo=>{
								const{data:res} = backinfo
								//console.log(res)
								if (res.err_code == 0) {
									this.editForm.manager_name=res.data.manager_name
									this.editForm.manager_email=res.data.manager_email
									this.editForm.manager_password=res.data.manager_password				
								}else{
									return this.$message.error(res.message)
									
									
								}
								
							})
				
				
			},
			//编辑对话框的确定按钮触发保存
			editmanager(){			
				this.$refs.editFormRef.validate(valid=>{
					if(!valid){//格式验证不对，res为flase
						return
					}
					console.log("用户编辑表单验证成功")
					var res= this.$axios
										.put('/manager/'+this.editmanagerId,this.editForm)		
										.then((backinfo)=>{									
										//	console.log(backinfo)	
											const {data:res}=backinfo
											if(res.err_code==1){
												//编辑用户失败
												this.$message.error(res.message)
											}else{
												//编辑用户成功
												this.$message.success(res.message)
												this.editDialogVisible = false 
												this.getmanagerList(1,'')
										    }
										})					
				})
				
			}



		}

	}
</script>

<style lang="less" scoped>

</style>
